package jay.home.bet.web.struts;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jay.home.bet.domain.Item;
import jay.home.bet.domain.Order;
import jay.home.bet.domain.User;
import jay.home.bet.misc.Util;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class DobetAction extends BaseAction
{
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{
		String username = request.getRemoteUser();
		if(username == null)return mapping.findForward("error");
		DynaActionForm df = (DynaActionForm) form;
		
		Double amount;
		try
		{
			amount = Double.parseDouble(df.getString("amount"));//获取用户下注额
		} catch (Exception e)
		{
			return mapping.findForward("error");
		}
		Order order = (Order)request.getSession().getAttribute("cart");
		
		if(order == null)return mapping.findForward("error");
		if(amount == null)return mapping.findForward("error");
		
		Double max = Double.valueOf(getBasicService().getConfig("bet.max.amount"));
		Double min = Double.valueOf(getBasicService().getConfig("bet.min.amount"));
		Double umoney = getaccountService().getUser(username).getBalance();
		
		//下注额超过用户余额
		if(amount > umoney)
		{
			request.setAttribute("message", "下注额超过用户余额");
			return mapping.findForward("error");
		}
		//下注额小于最小限额
		if(amount < min)
		{
			request.setAttribute("message", "下注额小于最小限额");
			return mapping.findForward("error");
		}
		//下注额超过最大限额
		if(amount > max)
		{
			request.setAttribute("message", "下注额超过最大限额");
			return mapping.findForward("error");
		}
		
		boolean pass = true;
		Order temp = copy(order);
		for(Item item : order.getItems())
		{
			boolean gunqiu = item.getCurrentScore() != null;
			boolean guoguan = !order.getIsDanshi();
			String oddType = item.getCounter();
			String eventid = item.getEventid();
			Item i = getOrderService().generateOrderItem(temp, eventid, oddType, guoguan, gunqiu);
			if(!checkPass(item, i))
				pass = false;
			i.setEvent(item.getEvent());
			i.setOrderid(temp.getId());
			temp.getItems().add(i);
		}
		if(!pass)
		{
			request.setAttribute("message", "服务器上的赔率信息已经发生变化");
			temp.setAmount(amount);
			request.getSession().setAttribute("cart", temp);
			return mapping.findForward("confirm");
		}
		
		int mincomp = Util.toInt(getBasicService().getConfig("bet.guoguan.min.comp"));
		if(mincomp == 0) mincomp = 2;
		int maxcomp = Util.toInt(getBasicService().getConfig("bet.guoguan.max.comp"));
		if(maxcomp == 0) maxcomp = 9;
		
		if ("过关".equals(order.getCat()))
		{
			if (order.getItems().size() < mincomp)
			{
				request.setAttribute("message", "过关数目小于最小限制:" + mincomp);
				return mapping.findForward("error");
			}
			if (order.getItems().size() > maxcomp)
			{
				request.setAttribute("message", "过关数目大于最大限制:" + maxcomp);
				return mapping.findForward("error");
			}
		}
		
		//通过检测， 下注成功 :)
		order.setAmount(amount);
		getOrderService().insertOrder(order, getaccountService().getUser(username));
		//清空用户的购物车
		request.getSession().removeAttribute("cart");
		return mapping.findForward("SUCCESS");
	}

	private boolean checkPass(Item item, Item i)
	{
		if(!item.getCat().equals(i.getCat()))return false;
		if(item.getAttach() != null)
			if(!item.getAttach().equals(i.getAttach()))return false;
		if(!item.getBeton().equals(i.getBeton()))return false;
		if(!item.getCounter().equals(i.getCounter()))return false;
		if(item.getCurrentScore()!=null)
			if(!item.getCurrentScore().equals(i.getCurrentScore()))return false;
		if(!item.getEventid().equals(i.getEventid()))return false;
		if(item.getHalf()!= null)
			if(!item.getHalf().equals(i.getHalf()))return false;
		if(!item.getOdd().equals(i.getOdd()))return false;
		if(item.getPankou() != null)
			if(!item.getPankou().equals(i.getPankou()))return false;
		if(item.getResultScore()!=null)
			if(!item.getResultScore().equals(i.getResultScore()))return false;
			
		return true;
	}

	private Order copy(Order order)
	{
		Order t = getOrderService().newOrder(order.getUsername(), order.getIsDanshi());
		t.setAlive(order.getAlive());
		t.setAmount(order.getAmount());
		t.setCanWin(order.getCanWin());
		t.setCat(order.getCat());
		t.setSettled(order.getSettled());
		t.setTime(new Date());
		return t;
	}
}
